/* ******************************************************************
 * This file is part of the escal project and distributed under the
 * terms of the GNU General Public License Version 3
 * Copyright (C) 2008 Epocher (xijiaosdq@gmail.com)
 * See COPYING file for licensing information.
 *******************************************************************/

#include "escal.h"
#include "folder_list_model.h"
#include "escal_settings.h"
#include <QFileDialog>
#include <QMessageBox>

using namespace Escal;
FolderListModel::FolderListModel(QObject *parent)
	:QAbstractItemModel(parent)
{
	columnHeaders << tr("Folder");
	foldersChanged();
	dirDialog = new QFileDialog();
        dirDialog->setFileMode(QFileDialog::DirectoryOnly);
	dirDialog->setAcceptMode(QFileDialog::AcceptOpen);
};

int FolderListModel::columnCount(const QModelIndex &parent) const{
	//if(parent==QModelIndex()){
		return columnHeaders.size();
	//}else{
	//	return 0;
	//}
};

int FolderListModel::rowCount(const QModelIndex &parent) const{
	if(parent.isValid())
		return 0;
	return folderList.size();
};

QVariant FolderListModel::data(const QModelIndex &index, int role) const{
	if (!index.isValid())
		return QVariant();

	if (role != Qt::DisplayRole)
		return QVariant();

	if (index.column() !=0 || index.row() > rowCount() -1){
		return QVariant();
	}
	return folderList[index.row()];
};

Qt::ItemFlags FolderListModel::flags(const QModelIndex &index) const{
	if (!index.isValid())
		return 0;
	return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
};

QVariant FolderListModel::headerData(int column, Qt::Orientation orientation,
	int role) const
{
	if (orientation == Qt::Horizontal && role == Qt::DisplayRole){
		return columnHeaders[column];
	}
	return QVariant();
};

QModelIndex FolderListModel::index(int row, int column,
	const QModelIndex &parent ) const 
{
	if(parent == QModelIndex()){
		return createIndex (row, column);
	}
	else{
		return QModelIndex();
	}
}

QModelIndex FolderListModel::parent(const QModelIndex &index) const
{
	return QModelIndex();
}

void FolderListModel::newFolder(){
	for(;;){
		if(!dirDialog->exec()){
			break;
		}
		QString folderName=dirDialog->selectedFiles()[0];
		while(folderName.endsWith("/") && folderName.size()>1){
			qDebug() << "folderName.endsWith(\"/\")";
			folderName.resize(folderName.size()-1);
		}
		QDir dir(folderName);
		if(!dir.mkdir(".egear_test_access") ||
					!dir.rmpath(".egear_test_access")){
			QMessageBox::critical(NULL, tr("warning"),
				tr("directory not writable!"));
			continue;
		}
		QString oldFolder;
		foreach(oldFolder, folderList){
			if(oldFolder==folderName){
				int row = folderList.indexOf(oldFolder);
				beginRemoveRows(QModelIndex(), row, row);
				folderList.removeAt(row);
				endRemoveRows();
			}
		}
		beginInsertRows(QModelIndex(), 0, 0);
		folderList.insert(0, folderName);
		endInsertRows();
		syncFolderList();
		break;
    	}
};

void FolderListModel::deleteFolder(const QModelIndex &index){
	int row = index.row();
	if(folderList.size()==1){
		QMessageBox::warning(NULL, "Please leave at least one folder.",
			"I need at least one folder to download.\n"
			"Please add another folder before you delete it.");
		return;
	}
	beginRemoveRows(QModelIndex(), row, row);
	folderList.removeAt(row);
	endRemoveRows();
	syncFolderList();
};

void FolderListModel::moveUpFolder(const QModelIndex &index){
	int row = index.row();
	if(row<=0) return;
	QString upperFolder = folderList[row-1];
	beginRemoveRows(QModelIndex(), row-1, row-1);
	folderList.removeAt(row-1);
	endRemoveRows();
	beginInsertRows(QModelIndex(), row, row);
	folderList.insert(row, upperFolder);
	endInsertRows();
	syncFolderList();
};

void FolderListModel::moveDownFolder(const QModelIndex &index){
	int row = index.row();
	if(row>=folderList.size()-1) return;
	QString upperFolder = folderList[row+1];
	beginRemoveRows(QModelIndex(), row+1, row+1);
	folderList.removeAt(row+1);
	endRemoveRows();
	beginInsertRows(QModelIndex(), row, row);
	folderList.insert(row, upperFolder);
	endInsertRows();
	syncFolderList();
};

void FolderListModel::moveToTopFolder(const QModelIndex &index){
	int row = index.row();
	if(row<=0) return;
	
	for(;row>0;row--){
		QString upperFolder = folderList[row-1];
		beginRemoveRows(QModelIndex(), row-1, row-1);
		folderList.removeAt(row-1);
		endRemoveRows();
		beginInsertRows(QModelIndex(), row, row);
		folderList.insert(row, upperFolder);
		endInsertRows();
	}
	syncFolderList();
};

void FolderListModel::moveToBottomFolder(const QModelIndex &index){
	int row = index.row();
	if(row>=folderList.size()) return;
	for(;row<folderList.size()-1;row++){
		QString upperFolder = folderList[row+1];
		beginRemoveRows(QModelIndex(), row+1, row+1);
		folderList.removeAt(row+1);
		endRemoveRows();
		beginInsertRows(QModelIndex(), row, row);
		folderList.insert(row, upperFolder);
		endInsertRows();
	}
	syncFolderList();
};


void FolderListModel::foldersChanged(){
	folderList = settings->value("Folder/DownloadFolderList").toStringList();
	reset();
};

void FolderListModel::syncFolderList(){
	settings->setValue("Folder/DownloadFolderList", folderList);
};
